<script setup>
import { getQueryString } from "@/utils/common";
import { showToast } from "vant";

const u = navigator.userAgent;
const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端

const getUrl = () => {
  const url = window.location.href;
  const urlArr = url.split("?");
  const code = getQueryString("code", urlArr[1]);
  const goodsId = getQueryString("goodsId", urlArr[2]);
  const inviteCode = getQueryString("inviteCode", urlArr[2]).split("#")[0];
  let xhr;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  }
  const param = new FormData();
  param.append("goodsId", goodsId);
  param.append("inviteCode", inviteCode);
  param.append("code", code);
  xhr.open(
    "post",
    "https://msapi.maishou88.com/api/v1/web/cloudPddGrant/sharePddGiftGoods",
    false
  );
  xhr.onload = function () {
    const result = JSON.parse(xhr.responseText);
    if (result.code == 200) {
      const kl = result?.data?.kl;
      if (!kl) {
        showToast("链接错误");
      } else {
        if (isAndroid) {
          window.open(kl);
        } else {
          let a = document.createElement("a");
          a.href = kl;
          a.click();
        }
      }
    } else {
    }
  };
  xhr.send(param);
};

onMounted(() => {
  //首页跳转返回 IOS不兼容页面自动刷新问题
  if (!isAndroid) {
    window.onpageshow = function (event) {
      if (event.persisted) {
        window.location.reload();
      }
    };
  }
  getUrl();
});
</script>
